<?php
require_once 'lib/controllerWithoutGetOrPost.php';
require_once 'view/standard-personal-noleftbar-top.php';
include('dataformatting.php');
class visitorsDB
{
    var $_db;
    public function __construct($db)
    {
        $this->_db = $db;
    }
    public function getName($personId)
    {
        $db = $this->_db;
        $personName = array();
        $sql = <<< EOT
            SELECT FirstNames, Surname
            FROM Person 
            WHERE ID = $personId

EOT;
        $result = $db->query($sql);
        if(!empty($result)) {
            $row = $db->fetch($result);
            $personName['firstNames'] = $db->unescape($row['FirstNames']);
            $personName['surname'] = $db->unescape($row['Surname']);
        }
        return $personName;
    }
}
class visitorsView
{
    var $_html;
    public function __construct($html)
    {
        $this->_html = $html;
    }
    public function headerName($firstNames, $surname)
    {
        $html = $this->_html;
        $firstNames = $html->text($firstNames);
        $surname = $html->text($surname);
        return <<< EOT
        <h2>$firstNames $surname</h2>

EOT;
    }
}
$visitorsDB = new VisitorsDB($db);
$view = new visitorsView($html);
function commentNames($firstname, $lastname) {
	if(!empty($firstname) && !empty($lastname)) {
		return $lastname.", ".$firstname;
	} else if(!empty($firstname)) {
		return $firstname;
	} else if(!empty($lastname)) {
		return $lastname;
	} else {
		return "";
	}
}

function commentCountry($country) {
	/*if(!empty($city) && !empty($country)) {
		return $city.", ".$country.". ";
	} else if(!empty($city)) {
		return $city.". ";
	} else */	
	if(!empty($country)) {
		return $country;
	} else {
		return "";
	}
}

function clearFirstName($val) {
 if($val == "First Name") {
	return "Anonymous";
 } else {
	return $val;
 }
}

function clearSurname($val) { 
 if($val == "Last Name") {
	return "";
 } else {
	return $val;
 }
}

function clearTown($val) {
 if($val == "Town") {
	return "";
 } else {
	return $val;
 }
}

function clearCountry($val) {
    if($val == "not") {
        return 0;
    } else {
        return $val;
    }
}

function clipartDefault($val) {
	if($val == null || $val == "") {
		return 0;
	} else {
		return $val;
	}
}
function FirstNames($value) {
	if(isset($value) && $value != "") {
		return "<li><span>First Names: </span>".$value."</li>";
	}
}

function KnownAs($value) {
	if(isset($value) && $value != "") {
		return	"<li><span>Known as:</span>".$value."</li>";
	}
}

function LastName($value) {
	if(isset($value) && $value != "") {
		return "<li><span>Last Name:</span>".$value."</li>";
	}
}

function Nee($value) {
	if(isset($value) && $value != "") {
		return " nee ".$value;
	}
	return "";
}

function OtherNames($value, $nee) {
	if((isset($value) && $value != "") || (isset($nee) && $nee != "")) {
		return "<li><span>Other Names:</span>".$value.Nee($nee)."</li>";
	}
}

function formatdatetime($date) {
	if($date == "0000-00-00 00:00:00") {
		return "";
	}   else {
		return date('d F Y', strtotime(str_replace("&", "&amp;", $date)));
	}
}

function BirthDate($value) {
	if(isset($value) && $value != "" && $value != "0000-00-00 00:00:00") {
		return "<li><span>Birth Date: </span>".formatdatetime($value)."</li>";
	}
}

function DeathDate($value) {
	if(isset($value) && $value != "" && $value != "0000-00-00 00:00:00") {
		return "<li><span>Death Date: </span>".formatdatetime($value)."</li>";
	}
}

function Age($value, $measure) {
	if(isset($value) && $value != "" && $value != "0") {
		return "<li><span>Age: </span>".$value.$measure."</li>";
	}
}

function Country($value) {
	if(isset($value) && $value != "") {
		return "<li><span>Country: </span>".$value."</li>";
	}
}

function CharityWeb($value, $http) {
	if(isset($http) && $http != "") {
		return "<a href=\"".$http."\">".$value."</a>";
	} else {
		return $value;
	}
}

function Charity($value, $http) {
	if(isset($value) && $value != "") {
		return "<li><span>Charity:</span> ".CharityWeb($value, $http)."</li>";
	}
}

?>
<script type="text/javascript">
function textCount() {
	elem = document.getElementById('comment');
	if (elem.value.length > 300) {
		elem.value = elem.value.substring(0, 300);
	} else {
		var counts = document.getElementById('count');
		counts.innerHTML = elem.value.length;
	}
}

function validate_email(field,alerttxt) {
	with (field) {
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) {
			alert("Please enter a valid email address.");
			return false;
		} else { return true; }
	}
}

function emailconfirm(field1, field2) {
	if(field1.value != field2.value && field1.value.length > 0 && field2.value.length > 0) {
		alert("Please double check that you have entered the same email address in both fields.")
	}
}


</script>
<?php			
$personId = intval($_GET['personid']);
$person = $visitorsDB->getName($personId);
if(!empty($person)) {
    ?>
    <div id="contentarea">
        <ul id="fullbar">
            <li class="header"><h2>List of Visitor Entries</h2></li>
    <?php 
    
    $querycount = "select count(*) from RemembranceComment left join Country on RemembranceComment.CountryID = Country.ID Where PersonID = '".mysql_real_escape_string($_GET['personid'])."' AND Category = '2'";
    $resultcount = mysql_query($querycount, $link);
    
    if (!$resultcount) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $querycount;
        die($message);
    }
                            
    echo "<li class=\"resultinfo\"><span>(".mysql_result($resultcount, 0).") listing results</span></li>";

    echo "<li class=\"messageheader\"><a href=\"remembrance.php?personid=".$_GET['personid']."\">Go back to the Remembrance Page for ".$person['firstNames']." ".$person['surname']."</a></li>";
    
    $querycomment = "select 	RemembranceComment.ID,
        RemembranceComment.PersonID, 
        RemembranceComment.Surname, 
        Country.Name as `Country`,
        RemembranceComment.FirstName, 
        RemembranceComment.CityTown, 
        RemembranceComment.DateAdded,
        RemembranceComment.DateModified,
        RemembranceComment.Email,
        RemembranceComment.Category,
        RemembranceComment.Heading from RemembranceComment left join Country on RemembranceComment.CountryID = Country.ID Where PersonID = '".mysql_real_escape_string($_GET['personid'])."' AND Category = '2' ORDER BY RemembranceComment.Surname ASC, RemembranceComment.FirstName ASC";


    $resultcomment = mysql_query($querycomment, $link);
    echo "<li class=\"visitorlist\"><ul>";
    while ($rowcomment = mysql_fetch_assoc($resultcomment)) {	
        echo "<li><span><a href=\"remembrance.php?personid=".$_GET['personid']."&commentid=".$rowcomment['ID']."\">".commentNames($rowcomment['FirstName'],$rowcomment['Surname'])."</a></span><span>".commentCountry($rowcomment['Country'])."</span><span class=\"date\">".date('d M Y', strtotime(str_replace("&", "&amp;", $rowcomment['DateAdded'])))."</span></li>";
    }
    echo "</ul></li>";
                    
                    
    ?>
				
			</ul>
			<ul id="rightbar">
				<li class="linkitem">
    <?php
    $query2 = "select count(*) from Publication left join PublicationPerson on Publication.ID = PublicationPerson.PublicationID where PublicationPerson.PersonID = ".$personId;
    $result2 = mysql_query($query2, $link);
    $query4 = "select `Name`, `DatePublished` from Publication left join PublicationPerson on Publication.ID = PublicationPerson.PublicationID where PublicationPerson.PersonID = ".$personId;
    $result4 = mysql_query($query4, $link);
    if(mysql_num_rows($result2)!=0) {
        echo "<h2><a href=\"#\">Published Notices (".mysql_result($result2, 0).")</a></h2>";
    } else {
        echo "<h2><a href=\"#\">Published Notices (0)</a></h2>";
    }

    echo "<ul>";
    if(mysql_num_rows($result4)!=0) {
        while ($row3 = mysql_fetch_assoc($result4)) {
            $pubname = explode("-",$row3['Name']);
            $pubname = $db->escape($pubname[2]);
            $query5 = "select `Name` from Newspapers where `ShortName` = '".$pubname."';";
            $result5 = mysql_query($query5, $link);
            echo "<li>".mysql_result($result5, 0)." - ".formatdatetime($row3['DatePublished'])."</li>";
        }				
    }
    echo "<li class=\"right\"><a href=\"personnotices.php?personid=".$_GET['personid']."\">view all</a></li>";
    echo "</ul>";			
                
    ?>
				</li>
				<li class="alt">
    <?php
    echo "<a title=\"Tell a Friend\" class=\"browsernav_tellfriend\" href=\"mailto:?subject=A Memory Tree has a Remembrance Page open for ".$person['firstNames'].", ".$person['surname']."&body=Hi, I'm letting you know that a Remembrance Page has been opened on www.amemorytree.co.nz website and you can leave a message for free today.  
Visit http://amemorytree.co.nz/remembrance.php?personid=".$_GET['personid']." to view the page and read other messages.\">Tell a Friend</a><br />";
    ?>
				</li>
			</ul>
		</div>
    
    <?php
}
include('view/standard-noads-bottom.php');	
?>
